<?php
namespace Tools\Controller;
use Common\Controller\HomebaseController;

/**
 * 留言板 TODO
 */
class GuestbookController extends HomebaseController{

	protected $guestbook_model;

	public function _initialize() {
		parent::_initialize();
		$this->guestbook_model=D("common/ToolsGuestbook");
	}

	public function index(){
		$model=M('tools_guestbook');
		// 分页
		// 获取总数据
		$tot=$model->where('status=0')->count();
		// echo $model->getLastSql();
		// 分配总数据
		$this->tot=$tot;
		// 实例化分页类
		$page=new \Think\Page($tot,5);
		// 获取单前页数
		$p=I("get.p/d",1);
		// 查询数据
		$data=$model->where('status=0')->order('createtime desc')->page("{$p},5")->select();
		$this->data=$data;
		// 分配页码
		$this->show=$page->show();
		// echo'<pre>';
		// print_r($data]);
		// echo'</pre>';
		$this->display();
	}

	//留言内容
	public function adMessage(){
		if(IS_POST){
			$username = I("name");
			$mobile = I("mobile");
			$msg = I('leave');
			$email=I('email');
			$createtime = time();
			if (!trim($username)){
				$this->error("姓名不能为空！");
			}

			if (!trim($mobile)){
				$this->error("手机号不能为空！");
			}

			if (!trim($msg)){
				$this->error("留言内容不能为空！");
			}

			$data['full_name'] = $username;
			$data['email']=$email;
			$data['mobile'] = $mobile;
			$data['msg'] = $msg;
			$data['createtime'] = date("Y-m-d H:i:s,$createtime");

			$guest =$this->guestbook_model->data($data)->add();

			if($guest){
				$this->success('留言成功！');
			}else{
				$this->error("留言失败！");
			}
		}
	}
}